package com.example.tt_bells_v02;

import com.wheel.ArrayWheelAdapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

public class WheelViewCapInt extends WheelViewCaptioned {
	
	ArrayWheelAdapter<String> adap;
	String values[] = null;
	Context con;
	int value_start = 0;
	int value_step = 1;
	int value_count = 10;
	
	public WheelViewCapInt(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		con = context;
		
		if (attrs != null) {
			TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.WheelViewCapInt);
			final int N = a.getIndexCount();
			for (int i = 0; i < N; ++i) {
				int attr = a.getIndex(i);
			    switch (attr) {
			        case R.styleable.WheelViewCapInt_value_start:
			            value_start = a.getInt(attr, 0);
			            break;
			        case R.styleable.WheelViewCapInt_value_step:
			            value_step = a.getInt(attr, 1);
			            break;
			        case R.styleable.WheelViewCapInt_value_count:
			        	value_count = a.getInt(attr, 10);
			        	break;
			    }

			}
			a.recycle();
			
			caption = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "text");
			if (caption != null) cap.setText(caption);
		
			refreshAdapter(true);
		}
	}
	
	public void setRange(int start, int step, int len) {
		if ((len <= 0) || (step <= 0)) return;
		
		boolean len_changed = false;
		
		if (len != value_count) len_changed = true;
		
		value_start = start;
		value_step = step;
		value_count = len;
		
		refreshAdapter(len_changed);
	}
	
	public void refreshAdapter(boolean is_len_changed) {
		if ((is_len_changed) || (values == null))
			values = new String[value_count];
	
		int val = value_start;
		for (int i=0; i<value_count; i++) {
			values[i] = Integer.toString(val);
			val+=value_step;
		}
		
		adap = new ArrayWheelAdapter<String>(con,values);
		wheel.setViewAdapter(adap);
	}

	int getCurrentValue() {
		return Integer.parseInt(values[wheel.getCurrentItem()]);
	}
}
